﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XCommunity.Data;
using XCommunity.Members;
using System.Web;

namespace XCommunity.Web
{

  /// <summary>
  /// 包含对帐户管理的静态方法。
  /// </summary>
  public static class AccountManage
  {

    private static AccountCrypto accountCrypto = new AccountCrypto();


    /// <summary>
    /// 登录用户到当前上下文
    /// </summary>
    /// <param name="username"></param>
    /// <param name="password"></param>
    /// <returns></returns>
    public static LoggedAccount Login( string username, string password )
    {
      return Login( username, AccountCrypto.ComputePasswordHash( password ) );
    }

    /// <summary>
    /// 登录用户到当前上下文
    /// </summary>
    /// <param name="username"></param>
    /// <param name="password"></param>
    /// <returns></returns>
    public static LoggedAccount Login( string username, byte[] password )
    {
      if ( !AccountDataProvider.ValidateAccount( ref username, password ) )
        return null;

      string token = accountCrypto.EncryptToString( username, password );

      LoggedAccount account = new LoggedAccount( username, token );

      AccountContext.Current.SignIn( account );

      return account;
    }



    public static WebAccountService Service
    {
      get { return WebCommunityContext.Current.AccountService; }
    }


    public static bool TryRegister( string username, string password, out string message )
    {

      if ( !Service.IsValidUsername( username ) )
      {
        message = "用户名不符合规范";
        return false;
      }

      if ( Service.IsRegistered( username ) )
      {
        message = "用户名已被注册";
        return false;
      }

      if ( !Service.Register( username, password ) )
      {
        message = "未知错误";
        return false;
      }

      if ( Login( username, password ) == null )
      {
        message = "注册成功，但登录失败";
        return false;
      }

      message = null;
      return true;
    }



    /// <summary>
    /// 从登陆凭据创建登陆帐户
    /// </summary>
    /// <param name="token"></param>
    /// <returns></returns>
    internal static LoggedAccount CreateAccount( string token )
    {
      string username;
      byte[] password;

      try
      {
        accountCrypto.DecryptAccount( token, out username, out password );
      }
      catch
      {
        return null;
      }

      if ( !AccountDataProvider.ValidateAccount( ref username, password ) )
        return null;

      return new LoggedAccount( username, token );
    }


    /// <summary>
    /// 登出当前帐户
    /// </summary>
    public static void Logout()
    {
      AccountContext.Current.SignOut();
    }



    private const string loginUrl = "~/Login.aspx?source={url}";

    public static void RedirectLogin()
    {
      Account account = AccountContext.Current.Account;

      if ( account != null && account.IsAuthenticated )
        return;

      var context = HttpContext.Current;

      string url = loginUrl.Replace( "{url}", HttpUtility.UrlEncode( context.Request.Url.ToString() ) );

      context.Response.Redirect( url.ToString(), true );

    }


  }
}
